<?php
namespace api\modules\v1\controllers;
use api\modules\v1\controllers\CoreController;
use common\models\Member;
use common\models\TeamTotal;
use common\models\TradeGrade;
use Yii;


use common\models\finance\TeamTotalModel;
/*
	会员相关控制器
*/


class MemberController extends CoreController
{
	/*
		会员首页 信息
	*/
	public function actionInfo() 
	{
		unset($this->_member['loginpwd']);
		unset($this->_member['paypwd']);
		$member = $this->_member;
		$data = array();
		$data['member_id'] = $this->_member['member_id'];
		$data['member_name'] = $this->_member['member_name'];
		$data['member_mobile'] = $this->_member['member_mobile'];
		$data['grade'] = $this->_member['grade'];
		$data['member_avatar'] = $this->_member['member_avatar'];

		$gradeConf = TradeGrade::gradeConf($this->request('lang'));
		$data['grade_name'] = $gradeConf[$this->_member['grade']];


		//订单信息
		// $orderInfo = Orders::getOrdersByGroupState($this->_memberId);
		// $orderNums = array();
		// foreach($orderInfo as $order){
		// 	$orderNums[$order['order_state']] = $order['total'];
		// }

		// $res = Orders::getOrderCount($this->_memberId,'4');
		// $orderNums['4'] = $res['total']?$res['total']:'';

		$this->out('用户信息',$data);  //array('order'=>$orderNums)
	}

	//修改用户信息
	public function actionEditInfo()
	{
		$data = array();
		if($this->request('member_avatar'))
			$data['member_avatar'] = $this->request('member_avatar');
		if($this->request('member_name'))
			$data['member_name'] = $this->request('member_name');

		$flag = Member::updateAll($data,['member_id'=>$this->_memberId]);
		if($flag) $this->out('修改成功');
		$this->error('修改失败');
	}


	/*
		* 修改密码
		* old_pass 原密码
		* password1 新密码
		* password2 确认密码
		* type 1登录密码 2交易密码
	*/
	public function actionEditPass()
	{
		$oldPass = $this->request('old_pass');

		if($this->request('password1')!=$this->request('password2'))
			$this->error('两次密码不一致');

		if(!$this->request('password1')) $this->error('密码不能为空');		

		if($this->request('type') == '1')
			if(md5($oldPass)!=$this->_member['loginpwd']) $this->error('原始密码错误');
		if($this->request('type') == '2')
			if(md5($oldPass)!=$this->_member['paypwd']) $this->error('原始密码错误');		


		$model = Member::findOne($this->_memberId);
		if($this->request('type') == '1') $model->loginpwd = md5($this->request('password1'));
		if($this->request('type') == '2') $model->paypwd = md5($this->request('password1'));
		$model->update_time = time();
		$flag = $model->save();

		if($flag) $this->out('操作成功');
		$this->error('操作失败');
	}	


	/*
		*获取团队成员
	*/
	public function actionGetTeamer()
	{
		$field = ['member_id','invite_id','member_name','member_mobile','grade','create_time'];
		$where = ['invite_id' => $this->_memberId];
		$children = Member::find()->select($field)->where($where)->asarray()->all();

		$gradeConf = TradeGrade::gradeConf($this->request('lang','cn'));
		if($children){
			foreach($children as &$child)
			{
				$child['grade_name'] = $gradeConf[$child['grade']];
			}
		}
		$extend = [];
		$teamTotal = TeamTotal::getTeamTotal($this->_memberId);
		$extend['team_total'] = isset($teamTotal['team_total'])?$teamTotal['team_total']:0;
		$this->out('我的团队',$children,$extend);
	}


	//获取用户团队业绩
	public function actionTeam_total()
	{
		$data = TeamTotalModel::getTeamTotal($this->_memberId);
		$this->out('团队业绩',$data);
	}

	public function actionMemberInfo(){
	    $userId = $oldPass = $this->request('member_id');
        $data = Member::getOne(['member_id'=>$userId]);
        $this->out('用户信息',$data);
    }


	
}